package springboot.springboot06.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.springboot06.Bean.Manager;
import springboot.springboot06.Bean.User;
import springboot.springboot06.Service.ManagerService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class indexController {

    @Autowired
    ManagerService managerService;


    @ResponseBody
    @GetMapping("/manas")
    public List<Manager> getManagers(){
        return managerService.getManagers();
    }

    @ResponseBody
    @RequestMapping("/add")
    public Manager saveManager(Manager manager){
        managerService.addManager(manager);
        return manager;
    }

    @ResponseBody
    @GetMapping("/manager")
    public Manager getById(Integer id){
        return managerService.getManager(id);
    }



    @GetMapping(value = {"/","/login"})
    public String indexpage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){
        if(StringUtils.hasLength(user.getUsername()) && StringUtils.hasLength(user.getPassword()) ){
            session.setAttribute("loginUser",user);
            /*登录成功后，进行重定向*/
            return "redirect:/main.html";
        }
        else{
            model.addAttribute("msg","登录失败，账号密码错误");
            return "login";
        }


    }
    /*
    *用于重定向
    */
    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model){

            return "main";

    }

}
